package com.jse.io.nio.network.new1.mine.test;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class Client1 {
 
    public void startClient() {
 
        try {
        	InetSocketAddress hostAddress = new InetSocketAddress("127.0.0.1", 999);
            SocketChannel client = SocketChannel.open(hostAddress);
     
            System.out.println("Client... started");
            
            String threadName = Thread.currentThread().getName();
     
            // Send messages to server
            String [] messages = new String [] 
            		{threadName + ": test1",threadName + ": test2",threadName + ": test3"};
     
            for (int i = 0; i < messages.length; i++) {
                byte [] message = new String(messages [i]).getBytes();
                ByteBuffer buffer = ByteBuffer.wrap(message);
                client.write(buffer);
                System.out.println(messages [i]);
                buffer.clear();
                Thread.sleep(5000);
            }
            
//            ByteBuffer readServerByte = ByteBuffer.allocate(1024);
//            client.read(readServerByte);
//            System.out.println(new String(readServerByte.array()));
//            client.close(); 
		} catch (Exception e) {
			e.printStackTrace();
		}           
    }
    
    public static void main(String[] args) throws Exception {
    	new Client1().startClient();
    	
    	Thread.sleep(3000);
	}
}

